<template>
  <!--编写页面静态部分，即view部分-->
  <div>
    <el-form :model="pageForm" label-width="80px" :rules="pageFormRules" ref="pageForm">
      <el-form-item label="所属站点" prop="siteId">
        <el-select v-model="pageForm.siteId" placeholder="请选择站点">
          <el-option
            v-for="item in siteList"
            :key="item.siteId"
            :label="item.siteName"
            :value="item.siteId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择模版" prop="templateId">
        <el-select v-model="pageForm.templateId" placeholder="请选择">
          <el-option
            v-for="item in templateList"
            :key="item.templateId"
            :label="item.templateName"
            :value="item.templateId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="页面名称" prop="pageName">
        <el-input v-model="pageForm.pageName" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="别名" prop="pageAliase">
        <el-input v-model="pageForm.pageAliase" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="访问路径" prop="pageWebPath">
        <el-input v-model="pageForm.pageWebPath" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="物理路径" prop="pagePhysicalPath">
        <el-input v-model="pageForm.pagePhysicalPath" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="数据URL" prop="dataUrl">
        <el-input v-model="pageForm.dataUrl" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-radio-group v-model="pageForm.pageType">
          <el-radio class="radio" label="0" >静态</el-radio>
          <el-radio class="radio" label="1">动态</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker type="datetime" placeholder="创建时间" v-model="pageForm.pageCreateTime">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="addSubmit">提交</el-button>
      <el-button  @click="go_back" >返回</el-button>
    </div>
  </div>
</template>
<script>
  import * as cmsApi from '../api/cms'

  /*编写页面静态部分，即model及vm部分。*/
  export default {
    data() {
      return {
        //站点列表
        siteList: [],
        //模板列表
        templateList: [],
        //新增界面数据
        pageForm: {
          siteId: '',
          templateId: '',
          pageName: '',
          pageAliase: '',
          pageWebPath: '',
          pageParameter: '',
          pagePhysicalPath: '',
          pageType: '0',
          pageCreateTime: new Date(),
          dataUrl:'',
        },
        pageFormRules: {
          siteId:[
            {required: true, message: '请选择站点', trigger: 'blur'}
          ],
          templateId:[
            {required: true, message: '请选择模版', trigger: 'blur'}
          ],
          pageName: [
            {required: true, message: '请输入页面名称', trigger: 'blur'}
          ],
          pageWebPath: [
            {required: true, message: '请输入访问路径', trigger: 'blur'}
          ],
          pagePhysicalPath: [
            {required: true, message: '请输入物理路径', trigger: 'blur'}
          ],
          dataUrl: [
            {required: true, message: '请输入数据URL', trigger: 'blur'}
          ]
        },
      }
    },
    methods: {
      //查询站点下拉菜单列表数据
      querySite: function(){
        cmsApi.select_site_list().then((res)=>{
          this.siteList = res.queryResult.list;
        })
      },
      queryTemplate: function(){
        cmsApi.select_template_list().then((res)=>{
          this.templateList = res.queryResult.list;
        })
      },
      addSubmit: function(){
        this.$refs['pageForm'].validate((valid) => {
          if (valid) {
            //表单校验成功
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              //调用page_add方法请求服务端新增接口
              cmsApi.page_add(this.pageForm).then((res) => {
                  if(res.success){
                   /* this.$message({
                      message: '提交成功',
                      type: 'success'
                    });*/
                    this.$message.success('提交成功');
                    //提交成功清空表单数据
                    this.$refs['pageForm'].resetFields();
                  }else if(res.message){
                    this.$message.error(res.message);
                  }else{
                    this.$message.error('提交失败');
                  }
                });
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      go_back(){
        this.$router.push({
          path: '/cms/page/list', query: {
            page: this.$route.query.page,
            siteId: this.$route.query.siteId//取出当前路由中的参数
          }
        })
      }
    },
    //调用钩子方法
    mounted() {
      this.querySite();
      this.queryTemplate();
    }
  }
</script>
<style>
  /*编写页面样式，不是必须*/
</style>
